package com.example.gravitysituation;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class CircuitTutorial extends Activity {

	// Tutorial parameters
	int numPages = 6;
	int currentPage;
	
	@Override
	// Set first page of tutorial as active view
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_circuit_tutorial);
		
		currentPage = 1;
		
		// Show first page
		ImageView imgView1 = (ImageView)findViewById(R.id.circuit_tutorial1);
		imgView1.setVisibility(View.VISIBLE);

		// Hide remaining pages
		for (int i = 2; i <=numPages; i++){
			String imageViewID = "circuit_tutorial" + i;
			int resID = getResources().getIdentifier(imageViewID, "id", "com.example.gravitysituation");
			ImageView imgView2 = (ImageView)findViewById(resID);
			imgView2 .setVisibility(View.INVISIBLE);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.circuit_tutorial, menu);
		return true;
	}
	
	public void runSimulation( View view ) {
    	Intent intent = new Intent( this, CircuitSelection.class);
    	startActivity(intent);
    }
	
	// Variables for swipe input
	float x1,x2;
    float y1,y2;
	    
    
    @Override	   
    // onTouchEvent () method gets called when User performs any touch event on screen 
    // Method to handle swipe event
    public boolean onTouchEvent(MotionEvent touchevent) {
         switch (touchevent.getAction()) {
            // when user first touches the screen, get x and y coordinate
             case MotionEvent.ACTION_DOWN: 
                 x1 = touchevent.getX();
                 y1 = touchevent.getY();
                 break;
               
             // user releases screen    
	         case MotionEvent.ACTION_UP: 
	        	 x2 = touchevent.getX();
	        	 y2 = touchevent.getY(); 
	        	 
	        	 String imageViewIDCurrent;
	        	 String imageViewIDNext;
	        	 int resIDCurrent;
	        	 int resIDNext;
	        	 int nextPage;
	        	 ImageView imgView1;
	        	 ImageView imgView2;
	        	 
	        	 

                 
	        	 // if left to right sweep motion	
                 if (x1 < x2) {
                	 if(currentPage > 1){
                		 // Set current page invisible
                    	 imageViewIDCurrent = "circuit_tutorial" + currentPage;
                    	 resIDCurrent = getResources().getIdentifier(imageViewIDCurrent, "id", "com.example.gravitysituation");
                    	 imgView1 = (ImageView)findViewById(resIDCurrent);
                         imgView1 .setVisibility(View.INVISIBLE);
                         
                         // Display next page
                		 nextPage = currentPage - 1;
	                     imageViewIDNext = "circuit_tutorial" + nextPage;
	                	 resIDNext = getResources().getIdentifier(imageViewIDNext, "id", "com.example.gravitysituation");
	                	 imgView2 = (ImageView)findViewById(resIDNext);
	                     imgView2 .setVisibility(View.VISIBLE);
	                     currentPage = nextPage;
                	 }
                  }
                
                 // if right to left sweep event on screen display next image
                 if (x1 > x2){
                	 if(currentPage < numPages){
                		 // Set current page invisible
                    	 imageViewIDCurrent = "circuit_tutorial" + currentPage;
                    	 resIDCurrent = getResources().getIdentifier(imageViewIDCurrent, "id", "com.example.gravitysituation");
                    	 imgView1 = (ImageView)findViewById(resIDCurrent);
                         imgView1 .setVisibility(View.INVISIBLE);
                         
                         // Display next page
                		 nextPage = currentPage + 1;
	                     imageViewIDNext = "circuit_tutorial" + nextPage;
	                	 resIDNext = getResources().getIdentifier(imageViewIDNext, "id", "com.example.gravitysituation");
	                	 imgView2 = (ImageView)findViewById(resIDNext);
	                     imgView2 .setVisibility(View.VISIBLE);
	                     currentPage = nextPage;
                	 }
                 }            
              
                 break;
                 
         }
         return false;
    }
}
